package webService;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import serverDB.Client;
import serverDB.ConnectToDataBase;
import serverManger.Server;
/**
 * share box with another client
 * @author sl300
 *
 */
public class ShareBoxServlet extends HttpServlet{
	/**
	 * Default serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/*
	 * get the Post request
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// redirect to logedIn2
		resp.sendRedirect("logedIn2.jsp");
		// get the session
		HttpSession session = req.getSession();
		// get the box name
		String boxName = req.getParameter("boxName");
		Server server = Server.getServerInst();
		ConnectToDataBase connectToDataBase = ConnectToDataBase.getInst();
		// get the client
		Client client = connectToDataBase.regetClient((Client)session.getAttribute("client"));
		String emailsNotSplit = req.getParameter("emails");
		String[] emails = emailsNotSplit.split(";|,|\r\n|\\s+");
		ArrayList<String> list = new ArrayList<String>();
		for(String s : emails)
			list.add(s);
		// share box
		server.shareBox(client, boxName, list);
		
	}
}
